var event = require('../../../../utils/event.js')
var regeneratorRuntime = require('../../../../lib/js/runtime-module.js')
import config from '../../../../utils/config2'
import util from '../../../../utils/util2'
import Serv from '../walletServ'
let TimerClass=util.TimerClass

let timerObj=null//计时器实例
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    isSendMsg: false,
    inputDisable: false,
    // 表单
    user_id: '',
    user_name: '',
    id_type: 'ID_CARD',
    id_no: '',
    reg_phone: '',
    password: {
    },
    random_key: '',
  },
  // 表单输入
  bindinput: function (e) {
    console.log('bindinput', e.currentTarget.dataset, e.detail)
    let label = e.currentTarget.dataset.label
    let value = e.detail.value
    this.setData({
      [label]: value,
    })
  },
  getWalletUser: async function (user_id) {
    //发起请求 user_id  user_type
    let params = {
      accountId: user_id,
      // appId: '',
    }
    let { success, msg, data } = await Serv.getWalletUser(params)

    if (!success) {
      wx.showToast({
        title: msg || '',
        icon: 'none',
      })
      return false
    }

    if (!data) {//未开通钱包
      this.setData({
        show: true,
        opacity: 'rgba(0,0,0,0.5)'
      })
      // wx.showModal({
      //   title: '提示',
      //   content: '请开通钱包！',
      //   // showCancel: false,
      //   success: function (res) {
      //     if (res.confirm) {
      //       wx.navigateTo({
      //         url: '../openWallet/openWallet',
      //       })
      //     } else {
      //     wx.switchTab({
      //       url:'../../my/mine/mine'
      //     })
      //     }

      //   }
      // })
    }
    //本地保存钱包基本信息
    if (data) {
      let { name = '', balance = '', certification, idCardNo, psettleBalance, psettleFrozen } = data || {}
      let mobile = wx.getStorageSync(config.phoneNoKey) || ''
      mobile = mobile.replace(/(\d{3})\d*(\d{4})/, '$1****$2')
      let newPsettleFrozen = (Number(psettleBalance) + Number(balance)).toFixed(2)
      //debugger
      wx.setStorageSync('walletInfo', {
        name, mobile, balance, idCardNo, psettleBalance, psettleFrozen: newPsettleFrozen
      })
      this.setData({
        balance, certification, psettleBalance, psettleFrozen: newPsettleFrozen
      })
    }

  },
  cancelApplyConfrim:function(){
    let user_id = wx.getStorageSync(config.userIdKey)
    let that  = this
    this.getWalletUser(user_id).then(()=>{
      let { psettleFrozen }  = this.data
      if (psettleFrozen){
        wx.showModal({
          title: '温馨提示',
          content: `卡里有钱：您的钱包余额还有￥${psettleFrozen}，请全部提现后再解绑。`,
          confirmText:'去提现',
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/subPackages2/pages/wallet/addIdentityCard/addIdentityCard',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }else{
        wx.showModal({
          title: '温馨提示',
          content: `卡里没钱：您确定要解绑吗？`,
          confirmText: '确认解绑',
          success(res) {
            if (res.confirm) {
              that.cancelApply()
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
      
    })

    
    
    
  },
  // 修改交易密码  user_id  verify_code  password	  random_key
  cancelApply: async function () {
    let { user_id, user_name, id_type, id_no, reg_phone, password, random_key, fromCome, cardLength} = this.data
   
    //发起请求
    let params = {
      user_id,
      user_name,
      id_type,
      id_no,
      reg_phone,
      password: password.pwdMw || '',
      random_key: password.randomKey || '',
    }
    let {success, msg, data} = await Serv.cancelApply(params)
    if (!success) {
      wx.showToast({
        title: msg || '',
        icon: 'none',
      })
      return false
    }
  
    wx.showModal({
      title: '提示',
      content: '操作成功',
      showCancel: false,
      complete: (res)=> {
        wx.redirectTo({
          url: '../index/index',
        })
      }
    })

  },
  
  //唤起密码控件
  pwdFocus: function (e) {
    let label = e.currentTarget.dataset.label
    // from：当前地址， eventName: 监听定义的事件名
    util.navigateStateChange(this, 'inputDisable')
    wx.navigateTo({
      url: '../../pwdCtrl/pwdCtrl?from=/wallet/cancleAccount/cancleAccount&eventName=onPwdChange' + `&label=${label}`
    })
  },
  //密码控件回调
  pwdCtrlCallback: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_id = wx.getStorageSync(config.userIdKey)
    let phoneNo = (wx.getStorageSync('walletInfo')||{}).mobile
    let { cardLength, fromCome } = options
    this.setData({ user_id, phoneNo, fromCome, cardLength})
  
    //监听密码控件输入
    var that = this
    event.on('onPwdChange', that, function (changedData) {
      let {pwdMw = '', label = '', randomKey = '',pwdHash='',pwdlen=0} = changedData
      console.log('DataChanged trigger...', changedData, {
        [label]: pwdMw
      })
      that.setData({
        [label]: {
          pwdMw: pwdMw,
          randomKey: randomKey,
          pwdHash: pwdHash,
          pwdlen: pwdlen,
          pwdMw_new: pwdMw.substr(0,pwdlen),
        },
      })
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    console.log('pages:', pages, 'currentPage:', currentPage)
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    //取消监听
    event.remove('onPwdChange', that)
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //
  // }
})
